package com.service;

import com.dao.OrderDao;
import com.dao.UserDao;
import com.utils.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class BuyService {

    UserService userService = new UserService();
    OrderService orderService = new OrderService();

    public void buy(int total){
        try{
            Connection conn = DBUtils.getConn();
            conn.setAutoCommit(false);//设置不自动提交
            userService.subBalance(total);
            orderService.createOrder();
            conn.commit();//提交

        }catch (Exception e){
            try {
                System.out.println("执行回滚....");
                DBUtils.conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            DBUtils.close();
        }
    }


}
